$(function () {

    var depot_datagrid = $("#depot_datagrid");
    var depot_dialog = $("#depot_dialog");
    var depot_form = $("#depot_form");


    /*easyui的数据列表组件初始化*/
    depot_datagrid.datagrid({
        url: "/depot/query.do",
        fit: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        sortName: 'hireDate',
        sortOrder: 'desc',
        toolbar: "#depot_toolbar",
        columns: [[
            {field: 'id', title: 'id', width: 100, checkbox: true},
            {field: 'name', title: '仓库名称', width: 100},
            {field: 'sn', title: '仓库编码', width: 100},
            {
                field: 'status', title: '状态', width: 100, formatter: function (value, row, index) {
                    return value ? "<font color='green'>启用</font>" : "<font color='red'>暂停</font>"
            }
            },
            {field: 'phone', title: '电话', width: 100},
            {field: 'address', title: '仓库地址', width: 100},
            {field: 'inputTime', title: '录入时间', width: 100}
        ]],
        onSelect:function (index, row) {
            //判断仓库的状态来显示按钮文字
            if (row.status) {
                $('#changeState_btn').linkbutton({
                    text:'禁用'
                });
            }else{
                $('#changeState_btn').linkbutton({
                    text:'启用'
                });
            }
        }
    })

    /*弹出编辑框的初始化*/
    depot_dialog.dialog({
        width: 270,
        height: 300,
        closed: true,
        buttons: [{
            text: "保存",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                methodObj["save"]();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObj["cancel"]();
            }
        }],
        onClose: function () {
            depot_form.form('clear');
        }
    })

    /*事件对象*/
    var methodObj = {

        /*新增仓库按钮事件*/
        add: function () {
            depot_dialog.dialog('setTitle', "新增仓库");
            depot_dialog.dialog('open');
        },

        /*编辑仓库按钮事件*/
        edit: function () {
            depot_dialog.dialog('setTitle', "编辑仓库");
            /*c查看是否有选中的仓库*/
            var row = depot_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'error');
                return;
            }
            /*数据回显*/
            depot_form.form('load', row);
            /*打开弹出框*/
            depot_dialog.dialog('open');
        },

        /*表单保存事件*/
        save: function () {
            $.messager.confirm("温馨提示", "确定要保存吗?", function () {
                depot_form.form('submit', {
                    url: "/depot/saveOrUpdate.do",
                    success: function (data) {
                        /*easyui的表单响应数据不是json,需要转换*/
                        data = $.parseJSON(data);
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info", function () {
                                depot_dialog.dialog('close');
                                depot_datagrid.datagrid('reload');
                            })
                        } else {
                            $.messager.alert("温馨提示", "操作失败", "error")
                        }
                    }
                })
            })
        },
        changeState: function () {
            //判断是否有选中数据
            var row = depot_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选中一条数据!', 'warning');
                return;
            }
            //弹出确认框
            $.messager.confirm('温馨提示', '您想要执行该操作吗?', function (y) {
                if (y) {
                    $.get("/depot/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info', function () {
                                depot_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'error');
                        }
                    }, "json")
                }
            });
        },

        /*表单刷新按钮*/
        reload: function () {
            depot_datagrid.datagrid("reload");
        },

        /*表单取消事件*/
        cancel: function () {
            depot_dialog.dialog('close');
        }
    }

    /*统一绑定事件*/
    $("a[data-method]").click(function () {
        var method = $(this).data("method");
        methodObj[method]();
    })
})
